package com.wetao.lib_common.greendao;

import android.content.Context;
import android.text.TextUtils;

import com.wetao.lib_common.Constants;
import com.wetao.lib_common.finalParam.IsAnswerSheetType;
import com.wetao.lib_common.greendao.dao.AnswerCardDao;
import com.wetao.lib_common.greendao.dao.AssistantExamDao;
import com.wetao.lib_common.greendao.dao.AssistantExamOnlineDao;
import com.wetao.lib_common.greendao.dao.AssistantQuestionDao;
import com.wetao.lib_common.greendao.dao.AssistantQuestionOnlineDao;
import com.wetao.lib_common.greendao.dao.DaoSession;
import com.wetao.lib_common.greendao.dao.ExamListenDao;
import com.wetao.lib_common.greendao.dao.ExaminationPointItemDao;
import com.wetao.lib_common.greendao.dao.FlexiImgDao;
import com.wetao.lib_common.greendao.dao.HttpCacheItemDao;
import com.wetao.lib_common.greendao.dao.NoticeItemDao;

import com.wetao.lib_common.greendao.dao.PageGridDao;
import com.wetao.lib_common.greendao.dao.PagePictureDao;
import com.wetao.lib_common.greendao.dao.PagePictureGridDao;
import com.wetao.lib_common.greendao.dao.PagePictureOnlineDao;
import com.wetao.lib_common.greendao.dao.PenOffLineGridInfoChildDao;
import com.wetao.lib_common.greendao.dao.PenOffLineGridInfoDao;
import com.wetao.lib_common.greendao.dao.QuestionGridDao;
import com.wetao.lib_common.greendao.dao.QuestionRevisionItemDao;
import com.wetao.lib_common.greendao.dao.ReadPushRecordDao;
import com.wetao.lib_common.greendao.dao.StudentIdToEnDao;
import com.wetao.lib_common.greendao.dao.UserItemDao;
import com.wetao.lib_common.greendao.dao.WireExamDao;
import com.wetao.lib_common.greendao.dao.WireQuestionDao;
import com.wetao.lib_common.greendao.dao.WireQuestionPictureDao;
import com.wetao.lib_common.greendao.model.AnswerCard;
import com.wetao.lib_common.greendao.model.AssistantExam;
import com.wetao.lib_common.greendao.model.AssistantExamOnline;
import com.wetao.lib_common.greendao.model.AssistantQuestion;
import com.wetao.lib_common.greendao.model.AssistantQuestionOnline;
import com.wetao.lib_common.greendao.model.ExamListen;
import com.wetao.lib_common.greendao.model.ExaminationPointItem;
import com.wetao.lib_common.greendao.model.FlexiImg;
import com.wetao.lib_common.greendao.model.HttpCacheItem;
import com.wetao.lib_common.greendao.model.NoticeItem;

import com.wetao.lib_common.greendao.model.PagePicture;
import com.wetao.lib_common.greendao.model.PagePictureGrid;
import com.wetao.lib_common.greendao.model.PagePictureOnline;
import com.wetao.lib_common.greendao.model.PenOffLineGridInfo;
import com.wetao.lib_common.greendao.model.PenOffLineGridInfoChild;
import com.wetao.lib_common.greendao.model.QuestionRevisionItem;
import com.wetao.lib_common.greendao.model.ReadPushRecord;
import com.wetao.lib_common.greendao.model.StudentIdToEn;
import com.wetao.lib_common.greendao.model.UserItem;
import com.wetao.lib_common.greendao.model.WireExam;
import com.wetao.lib_common.greendao.model.WireQuestion;
import com.wetao.lib_common.greendao.model.WireQuestionPicture;
import com.wetao.lib_common.utils.FileUtils;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.MyFileFilter;
import com.wetao.lib_common.utils.SPUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class DBUtils {
    private static final String TAG = "DBUtils";
    private static volatile DBUtils instance;
    private DaoSession mDaoSession;
    private GreenDaoHelper helper;
    private Context context;

    public DBUtils(Context context) {
        this.context = context;
    }

    public static DBUtils getInstance(Context context) {
        if (instance == null) {
            synchronized (DBUtils.class) {
                if (null == instance) {
                    // this.context = context.getApplicationContext();
                    instance = new DBUtils(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public DaoSession getDaoSession() {
        if (null == mDaoSession) {
            helper = new GreenDaoHelper(context);
            mDaoSession = helper.initDao(SPUtils.getInstance().getString(Constants.USER_ID_KEY, "00"));
        }
        return mDaoSession;
    }


    public void close() {
        if (helper != null) {
            helper.close();
        }
    }

    public void clear() {
        if (instance != null) {
            instance = null;
        }
        if (mDaoSession != null) {
            mDaoSession = null;
        }
        if (helper != null) {
            helper = null;
        }
    }

    /*********************************NoticeItemDao**********************************************/
    private NoticeItemDao getNoticeItemDao() {
        return getDaoSession().getNoticeItemDao();
    }

    public synchronized long insertNotice(NoticeItem item) {
        try {
            List<NoticeItem> list2 = getNoticeItemDao().queryBuilder()
                    .where(NoticeItemDao.Properties.NoticeId.eq(item.getNoticeId()))
                    .build()
                    .list();
            getNoticeItemDao().deleteInTx(list2);

            return getNoticeItemDao().insert(item);
        } catch (Exception e) {

        }
        return -1;
    }

    public synchronized void deleteNoticeItem(NoticeItem item) {
        getNoticeItemDao().delete(item);
    }

    public synchronized void deleteNoticeItemByNoticeId(String noticeId) {
        List<NoticeItem> list = null;
        list = getNoticeItemDao().queryBuilder()
                .where(NoticeItemDao.Properties.NoticeId.eq(noticeId))
                .build()
                .list();
        getNoticeItemDao().deleteInTx(list);
    }

    public synchronized NoticeItem getNoticeByNoticeId(String noticeId) {
        List<NoticeItem> list = getNoticeItemDao().queryBuilder()
                .where(NoticeItemDao.Properties.NoticeId.eq(noticeId))
                .build()
                .list();
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }


    public synchronized List<NoticeItem> getAllNotice() {
        List<NoticeItem> list = getNoticeItemDao().queryBuilder()
                .orderAsc(NoticeItemDao.Properties.Time)
                .build().list();
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }


    /********************************HttpCacheItemDap***********************************************/
    private HttpCacheItemDao getHttpCacheItemDao(){
        return getDaoSession().getHttpCacheItemDao();
    }

    public synchronized long insertHttpCacheItem(HttpCacheItem item){
        List<HttpCacheItem> list2 = null;
        try {
            if(TextUtils.isEmpty(item.getParams())) {
                list2 = getHttpCacheItemDao().queryBuilder()
                        .where(HttpCacheItemDao.Properties.Url.eq(item.getUrl()),
                                HttpCacheItemDao.Properties.Token.eq(item.getToken()),
                                HttpCacheItemDao.Properties.StudentId.eq(item.getStudentId()))
                        .build()
                        .list();
            }else{
                list2 = getHttpCacheItemDao().queryBuilder()
                        .where(HttpCacheItemDao.Properties.Url.eq(item.getUrl()),
                                HttpCacheItemDao.Properties.Token.eq(item.getToken()),
                                HttpCacheItemDao.Properties.Params.eq(item.getParams()),
                                HttpCacheItemDao.Properties.StudentId.eq(item.getStudentId()))
                        .build()
                        .list();
            }
            getHttpCacheItemDao().deleteInTx(list2);

            return getHttpCacheItemDao().insert(item);
        } catch (Exception e) {
            //Toast.makeText(this, "插入数据库失败", Toast.LENGTH_SHORT).show();

        }
        return -1;
    }

    public HttpCacheItem getHttpCacheItem(String userId, String token, String url, String params){
        List<HttpCacheItem> list = null;
        if(TextUtils.isEmpty(params)){
            list = getHttpCacheItemDao().queryBuilder()
                    .where(HttpCacheItemDao.Properties.Url.eq(url),
                            HttpCacheItemDao.Properties.Token.eq(token),
                            HttpCacheItemDao.Properties.StudentId.eq(userId))
                    .build()
                    .list();
        }else{
            list = getHttpCacheItemDao().queryBuilder()
                    .where(HttpCacheItemDao.Properties.Url.eq(url),
                            HttpCacheItemDao.Properties.Token.eq(token),
                            HttpCacheItemDao.Properties.Params.eq(params),
                            HttpCacheItemDao.Properties.StudentId.eq(userId))
                    .build()
                    .list();
        }

        if(null == list || list.size() ==0){
            return null;
        }
        return list.get(0);
    }


    /********************************ExaminationPointItem***********************************/
    private ExaminationPointItemDao getExaminationPointItemDao() {
        return getDaoSession().getExaminationPointItemDao();
    }

    public synchronized long insertExaminationPointItem(ExaminationPointItem item) {
        List<ExaminationPointItem> list = null;
        try{
            list = getExaminationPointItemDao().queryBuilder()
                    .where(ExaminationPointItemDao.Properties.SubjectId.eq(item.getSubjectId()))
                    .build()
                    .list();
            getExaminationPointItemDao().deleteInTx(list);
            return getExaminationPointItemDao().insert(item);
        }catch (Exception e){
            LogUtils.e("查询数据库报错" + e);
        }
        return -1;
    }

    public synchronized void updateExaminationPointItem(ExaminationPointItem item){
        getExaminationPointItemDao().update(item);
    }

    public ExaminationPointItem getExaminationPointBySubject(Integer subjectId){
        List<ExaminationPointItem> list = null;
        try{
            list = getExaminationPointItemDao().queryBuilder()
                    .where(ExaminationPointItemDao.Properties.SubjectId.eq(subjectId))
                    .build()
                    .list();
        }catch (Exception e){
            LogUtils.e("查询数据库报错" + e);
        }
        if (list == null || list.size() == 0){
            return null;
        }
        return list.get(0);
    }


    /********************************ReadPushRecordDao***********************************/
    private ReadPushRecordDao getReadPushRecordDao(){
        return getDaoSession().getReadPushRecordDao();
    }

    public synchronized long insertReadPushRecord(ReadPushRecord item){
        List<ReadPushRecord> list = null;
        try {
            list = getReadPushRecordDao().queryBuilder()
                    .where(ReadPushRecordDao.Properties.ReadPushId.eq(item.getReadPushId()))
                    .build()
                    .list();
            getReadPushRecordDao().deleteInTx(list);
            return getReadPushRecordDao().insert(item);
        }catch (Exception e){

        }
        return -1;
    }

    public synchronized void updateReadPushRecord(ReadPushRecord item){
        getReadPushRecordDao().update(item);
    }

    public synchronized void deleteReadPushRecord(ReadPushRecord item){
        getReadPushRecordDao().delete(item);
    }


    public ReadPushRecord getReadPushRecordById(Integer id){
        List<ReadPushRecord> list = null;
        try{
            list = getReadPushRecordDao().queryBuilder()
                    .where(ReadPushRecordDao.Properties.ReadPushId.eq(id))
                    .build()
                    .list();
        }catch (Exception e){
            LogUtils.e("查询数据库报错" + e);
        }
        if (list == null || list.size() == 0){
            return null;
        }
        return list.get(0);
    }





    /********************************QuestionRevisionItemDao***********************************/
    private QuestionRevisionItemDao getQuestionRevisionDao() {
        return getDaoSession().getQuestionRevisionItemDao();
    }

    public synchronized long insertQuestionRevisionItem(QuestionRevisionItem item) {
        List<QuestionRevisionItem> list = null;
        try{
            list = getQuestionRevisionDao().queryBuilder()
                    .where(QuestionRevisionItemDao.Properties.ExamId_studentExamAnswerId_questionId.eq(item.getExamId_studentExamAnswerId_questionId()))
                    .build()
                    .list();
            getQuestionRevisionDao().deleteInTx(list);
            return getQuestionRevisionDao().insert(item);
        }catch (Exception e){
            LogUtils.e("查询数据库报错" + e);
        }
        return -1;
    }

    public synchronized void updateQuestionRevisionItem(QuestionRevisionItem item){
        getQuestionRevisionDao().update(item);
    }


    public synchronized void deleteQuestionRevisionItem(QuestionRevisionItem item){
        getQuestionRevisionDao().delete(item);
    }

    public QuestionRevisionItem getQuestionRevisionByTag(String tag){
        List<QuestionRevisionItem> list = null;
        try{
            list = getQuestionRevisionDao().queryBuilder()
                    .where(QuestionRevisionItemDao.Properties.ExamId_studentExamAnswerId_questionId.eq(tag))
                    .build()
                    .list();
        }catch (Exception e){
            LogUtils.e("查询数据库报错" + e);
        }
        if (list == null || list.size() == 0){
            return null;
        }
        return list.get(0);
    }

    /********************************UserItemDao***********************************************/
    private UserItemDao getUserItemDao() {
        return getDaoSession().getUserItemDao();
    }

    public synchronized long insertUser(UserItem item) {
        try {
            return getUserItemDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }



    public void deleteAllUser() {
        getUserItemDao().deleteAll();
    }




    /********************************PenOffLineGridInfoDao***********************************************/
    private PenOffLineGridInfoDao getPenOffLineGridInfoDao() {
        return getDaoSession().getPenOffLineGridInfoDao();
    }

    public synchronized long insertPenOffLineGridInfo(PenOffLineGridInfo item) {
        try {
            List<PenOffLineGridInfo> list = getPenOffLineGridInfoDao().queryBuilder()
                    .where(PenOffLineGridInfoDao.Properties.PicId.eq(item.getPicId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0 && item.getPicId() != 0) {
                //getAssistantExamDao().deleteInTx(list);
                LogUtils.e("m1: insertPenOffLineGridInfo 插入数据库错误，已经有数据了");
                return -1;
            }
            return getPenOffLineGridInfoDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public PenOffLineGridInfo getPenOffLineGridInfoByPicId(int picId) {
        PenOffLineGridInfo penOffLineGridInfo = null;
        try {
            penOffLineGridInfo = getPenOffLineGridInfoDao().queryBuilder()
                    .where(PenOffLineGridInfoDao.Properties.PicId.eq(picId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return penOffLineGridInfo;
    }

    public List<PenOffLineGridInfo> getPenOffLineGridInfoByPicId0() {
        List<PenOffLineGridInfo> penOffLineGridInfo = null;
        try {
            penOffLineGridInfo = getPenOffLineGridInfoDao().queryBuilder()
                    .where(PenOffLineGridInfoDao.Properties.PicId.eq(0))
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return penOffLineGridInfo;
    }

    public List<PenOffLineGridInfo> getPenOffLineGridInfoByPageserial(String pageserial) {
        List<PenOffLineGridInfo> list = null;
        try {
            list = getPenOffLineGridInfoDao().queryBuilder()
                    .where(PenOffLineGridInfoDao.Properties.Pageserial.like("%" + pageserial + "%"))
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public List<PenOffLineGridInfo> getPenOffLineGridInfoAll() {
        List<PenOffLineGridInfo> list = null;
        try {
            list = getPenOffLineGridInfoDao().queryBuilder()
                    .where(PenOffLineGridInfoDao.Properties.IsWrite.eq(true),
                            PenOffLineGridInfoDao.Properties.IsAnswerSheet.eq(IsAnswerSheetType.SHEET_TYPE_2),
                            PenOffLineGridInfoDao.Properties.ExamId.isNotNull())
                    .orderAsc(PenOffLineGridInfoDao.Properties.ExamId, PenOffLineGridInfoDao.Properties.PicId)
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public List<PenOffLineGridInfo>  getPenOffLineGridInfoByExamId(int examId) {
        List<PenOffLineGridInfo> list = null;
        try {
            list = getPenOffLineGridInfoDao().queryBuilder()
                    .where(PenOffLineGridInfoDao.Properties.ExamId.eq(examId),
                            PenOffLineGridInfoDao.Properties.IsWrite.eq(true))
                    .orderAsc(PenOffLineGridInfoDao.Properties.PicId)
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public synchronized void deletePenOffLineGridInfoByExamid(int examId) {
        try {
            List<PenOffLineGridInfo> list = getPenOffLineGridInfoDao().queryBuilder()
                    .where(PenOffLineGridInfoDao.Properties.ExamId.eq(examId))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getPenOffLineGridInfoDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void deletePenOffLineGridInfo(PenOffLineGridInfo item) {
        try {
            getPenOffLineGridInfoDao().delete(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void deletePenOffLineGridInfoByPicId0() {
        try {
            List<PenOffLineGridInfo> list = getPenOffLineGridInfoByPicId0();
            if(list != null && list.size() > 0) {
                getPenOffLineGridInfoDao().deleteInTx(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updatePenOffLineGridInfoByExamid(int examId) {
        try {
            List<PenOffLineGridInfo> list = getPenOffLineGridInfoDao().queryBuilder()
                    .where(PenOffLineGridInfoDao.Properties.ExamId.eq(examId))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            for(PenOffLineGridInfo item : list) {
                item.setBeginTime(null);
                item.setFinishTime(null);
                item.setOssPath(null);
                item.setIsWrite(false);
                updatePenOffLineGridInfo(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updatePenOffLineGridInfo(PenOffLineGridInfo item) {
        getPenOffLineGridInfoDao().update(item);
    }


    /********************************AssistantExamDao***********************************************/
    private AssistantExamDao getAssistantExamDao() {
        return getDaoSession().getAssistantExamDao();
    }

    public synchronized long insertAssistantExam(AssistantExam item) {
        try {
            List<AssistantExam> list = getAssistantExamDao().queryBuilder()
                    .where(AssistantExamDao.Properties.ExamId.eq(item.getExamId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getAssistantExamDao().deleteInTx(list);
                LogUtils.e("m1: insertAssistantExam 插入数据库错误，已经有数据了");
                return -1;
            }
            return getAssistantExamDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public AssistantExam getAssistantExamById(long id) {
        AssistantExam assistantExam = null;
        try {
            assistantExam = getAssistantExamDao().queryBuilder()
                    .where(AssistantExamDao.Properties.Id.eq(id))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return assistantExam;
    }

    public AssistantExam getAssistantExamByExamId(int examId) {
        AssistantExam assistantExam = null;
        try {
            assistantExam = getAssistantExamDao().queryBuilder()
                    .where(AssistantExamDao.Properties.ExamId.eq(examId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return assistantExam;
    }

    public synchronized void deleteAssistantExamByExamid(int examId) {
        try {
            List<AssistantExam> list = getAssistantExamDao().queryBuilder()
                    .where(AssistantExamDao.Properties.ExamId.eq(examId))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getAssistantExamDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*public synchronized void deleteAssistantExamById(long id) {
        try {
            List<AssistantExam> list = getAssistantExamDao().queryBuilder()
                    .where(AssistantExamDao.Properties.Id.eq(id))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getAssistantExamDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/

    public synchronized void updateAssistantExam(AssistantExam item) {
        getAssistantExamDao().update(item);
    }


    /********************************AssistantQuestionDao***********************************************/
    private AssistantQuestionDao getAssistantQuestionDao() {
        return getDaoSession().getAssistantQuestionDao();
    }

    public synchronized long insertAssistantQuestion(AssistantQuestion item) {
        try {
            List<AssistantQuestion> list = getAssistantQuestionDao().queryBuilder()
                    .where(AssistantQuestionDao.Properties.QuestionId.eq(item.getQuestionId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getAssistantExamDao().deleteInTx(list);
                LogUtils.e("m1: insertAssistantQuestion 插入数据库错误，已经有数据了");
                return -1;
            }
            return getAssistantQuestionDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public AssistantQuestion getAssistantQuestionByQuestionId(Integer questionId) {
        AssistantQuestion assistantQuestion = null;
        try {
            assistantQuestion = getAssistantQuestionDao().queryBuilder()
                    .where(AssistantQuestionDao.Properties.QuestionId.eq(questionId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return assistantQuestion;
    }

    public List<AssistantQuestion> getAssistantQuestionByAssistantExamId(long assistantExamId) {
        List<AssistantQuestion> list = null;
        try {
            list = getAssistantQuestionDao().queryBuilder()
                    .where(AssistantQuestionDao.Properties.AssistantExamId.eq(assistantExamId))
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public synchronized void deleteAssistantQuestionByAssistantExamId(long assistantExamId) {
        try {
            List<AssistantQuestion> list = getAssistantQuestionDao().queryBuilder()
                    .where(AssistantQuestionDao.Properties.AssistantExamId.eq(assistantExamId))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getAssistantQuestionDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updateAssistantQuestion(AssistantQuestion item) {
        getAssistantQuestionDao().update(item);
    }

    /********************************AssistantQuestionOnlineDao***********************************************/
    private AssistantQuestionOnlineDao getAssistantQuestionOnlineDao() {
        return getDaoSession().getAssistantQuestionOnlineDao();
    }

    public synchronized long insertAssistantQuestionOnline(AssistantQuestionOnline item) {
        try {
            List<AssistantQuestionOnline> list = getAssistantQuestionOnlineDao().queryBuilder()
                    .where(AssistantQuestionOnlineDao.Properties.QuestionId.eq(item.getQuestionId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getAssistantExamDao().deleteInTx(list);
                LogUtils.e("m1: insertAssistantQuestion 插入数据库错误，已经有数据了");
                return -1;
            }
            return getAssistantQuestionOnlineDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public AssistantQuestionOnline getAssistantQuestionOnlineByQuestionId(Long assistantExamId, Integer questionId) {
        AssistantQuestionOnline assistantQuestion = null;
        try {
            assistantQuestion = getAssistantQuestionOnlineDao().queryBuilder()
                    .where(AssistantQuestionOnlineDao.Properties.AssistantExamId.eq(assistantExamId),
                            AssistantQuestionOnlineDao.Properties.QuestionId.eq(questionId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return assistantQuestion;
    }

    public List<AssistantQuestionOnline> getAssistantQuestionOnlineByAssistantExamId(long assistantExamId) {
        List<AssistantQuestionOnline> list = null;
        try {
            list = getAssistantQuestionOnlineDao().queryBuilder()
                    .where(AssistantQuestionOnlineDao.Properties.AssistantExamId.eq(assistantExamId))
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public synchronized void deleteAssistantQuestionOnlineByAssistantExamId(long assistantExamId) {
        try {
            List<AssistantQuestionOnline> list = getAssistantQuestionOnlineDao().queryBuilder()
                    .where(AssistantQuestionOnlineDao.Properties.AssistantExamId.eq(assistantExamId))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getAssistantQuestionOnlineDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updateAssistantQuestionOnline(AssistantQuestionOnline item) {
        getAssistantQuestionOnlineDao().update(item);
    }

    /********************************ExamListenDao***********************************************/
    private ExamListenDao getExamListenDao() {
        return getDaoSession().getExamListenDao();
    }

    public synchronized long insertExamListen(ExamListen item) {
        try {
            List<ExamListen> list = getExamListenDao().queryBuilder()
                    .where(ExamListenDao.Properties.ListeningUrl.like("%" + item.getListeningUrl() + "%"))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getAssistantExamDao().deleteInTx(list);
                LogUtils.e("m1: insertExamListen 插入数据库错误，已经有数据了");
                return -1;
            }
            return getExamListenDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public ExamListen getExamListenByUrl(String url) {
        ExamListen examListen = null;
        try {
            examListen = getExamListenDao().queryBuilder()
                    .where(ExamListenDao.Properties.ListeningUrl.like("%" + url + "%"))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return examListen;
    }

    public synchronized void deleteExamListenByAssistantExamId(long assistantExamId) {
        try {
            List<ExamListen> list = getExamListenDao().queryBuilder()
                    .where(ExamListenDao.Properties.AssistantExamId.eq(assistantExamId))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getExamListenDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updateExamListen(ExamListen item) {
        getExamListenDao().update(item);
    }

    /********************************PagePictureDao***********************************************/
    private PagePictureDao getPagePictureDao() {
        return getDaoSession().getPagePictureDao();
    }

    public synchronized long insertPagePicture(PagePicture item) {
        try {
            List<PagePicture> list = getPagePictureDao().queryBuilder()
                    .where(PagePictureDao.Properties.PictureId.eq(item.getPictureId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getAssistantExamDao().deleteInTx(list);
                LogUtils.e("m1: insertPagePicture 插入数据库错误，已经有数据了");
                return -1;
            }
            return getPagePictureDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public List<PagePicture> getPagePictureByPageserial(String pageserial) {
        List<PagePicture> list = null;
        try {
            list = getPagePictureDao().queryBuilder()
                    .where(PagePictureDao.Properties.Pageserial.like("%" + pageserial + "%"))
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public List<PagePicture> getPagePictureByAssistantExamId(long id) {
        List<PagePicture> list = null;
        try {
            list = getPagePictureDao().queryBuilder()
                    .where(PagePictureDao.Properties.AssistantExamId.eq(id))
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public PagePicture getPagePictureByPictureId(int pictureId) {
        PagePicture pagePicture = null;
        try {
            pagePicture = getPagePictureDao().queryBuilder()
                    .where(PagePictureDao.Properties.PictureId.eq(pictureId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pagePicture;
    }

    public PagePicture getPagePictureById(long id) {
        PagePicture pagePicture = null;
        try {
            pagePicture = getPagePictureDao().queryBuilder()
                    .where(PagePictureDao.Properties.Id.eq(id))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pagePicture;
    }

    public synchronized void deletePagePictureByAssistantExamId(long assistantExamId) {
        try {
            List<PagePicture> list = getPagePictureDao().queryBuilder()
                    .where(PagePictureDao.Properties.AssistantExamId.eq(assistantExamId))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getPagePictureDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updatePagePicture(PagePicture item) {
        getPagePictureDao().update(item);
    }

    /********************************PagePictureGridDao***********************************************/
    private PagePictureGridDao getPagePictureGridDao() {
        return getDaoSession().getPagePictureGridDao();
    }

    public synchronized long insertPagePictureGrid(PagePictureGrid item) {
        try {
            return getPagePictureGridDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public List<PagePictureGrid> getPagePictureGridByQuestionId(int questionId) {
        List<PagePictureGrid> list = null;
        try {
            list = getPagePictureGridDao().queryBuilder()
                    .where(PagePictureGridDao.Properties.QuestionId.eq(questionId))
                    .build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (list == null || list.size() == 0) {
            return null;
        }
        return list;
    }

    public synchronized void deletePagePictureGridByPagePictureId(long picId) {
        try {
            List<PagePictureGrid> list = getPagePictureGridDao().queryBuilder()
                    .where(PagePictureGridDao.Properties.PagePictureId.eq(picId))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getPagePictureGridDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updatePagePictureGrid(PagePictureGrid item) {
        getPagePictureGridDao().update(item);
    }


    /********************************PagePictureOnlineDao***********************************************/

    private PagePictureOnlineDao getPagePictureOnlineDao(){
        return getDaoSession().getPagePictureOnlineDao();
    }

    public synchronized long insertPagePictureOnline(PagePictureOnline item)
    {
        try {
            List<PagePictureOnline> list = getPagePictureOnlineDao().queryBuilder()
                    .where(PagePictureOnlineDao.Properties.PictureId.eq(item.getPictureId()))
                    .build()
                    .list();
            if(list != null && list.size() > 0) {
                //getAssistantExamDao().deleteInTx(list);
                LogUtils.e("m1: insertPagePictureOnline 插入数据库错误，已经有数据了");
                return -1;
            }
            return getPagePictureOnlineDao().insert(item);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return -1;
    }

    public PagePictureOnline getPagePictureOnlineByPageserial(long assistExamId, int questionId, String pageserial) {
        PagePictureOnline item = null;
        try {
            item = getPagePictureOnlineDao().queryBuilder()
                    .where(PagePictureOnlineDao.Properties.QuestionId.eq(questionId),
                            PagePictureOnlineDao.Properties.AssistantExamId.eq(assistExamId),
                            PagePictureOnlineDao.Properties.PageSerial.like("%" + pageserial + "%"))
                    .build()
                    .unique();

        }catch (Exception e){
            e.printStackTrace();
        }
        return item;
    }

    public List<PagePictureOnline> getPagePictureOnlineByPageserial(long assistExamId, int questionId) {
        List<PagePictureOnline> list = null;
        try {
            list = getPagePictureOnlineDao().queryBuilder()
                    .where(PagePictureOnlineDao.Properties.QuestionId.eq(questionId),
                            PagePictureOnlineDao.Properties.AssistantExamId.eq(assistExamId))
                    .build()
                    .list();

        }catch (Exception e){
            e.printStackTrace();
        }
        if (list == null || list.size() == 0 ) {
            return null;
        }
        return list;
    }



    public List<PagePictureOnline> getPagePicturesByQuestionId(Long assistantExamId, int questionId) {
        List<PagePictureOnline> list = null;
        try {
            list = getPagePictureOnlineDao().queryBuilder()
                    .where(PagePictureOnlineDao.Properties.QuestionId.eq(questionId),
                            PagePictureOnlineDao.Properties.AssistantExamId.eq(assistantExamId))
                    .orderAsc(PagePictureOnlineDao.Properties.AnswerIndex)
                    .build()
                    .list();

        }catch (Exception e){
            e.printStackTrace();
        }
        if (list == null || list.size() == 0 ) {
            return null;
        }
        return list;
    }

    public List<PagePictureOnline> getPagePicturesByQuestionId(Long assistantExamId) {
        List<PagePictureOnline> list = null;
        try {
            list = getPagePictureOnlineDao().queryBuilder()
                    .where(PagePictureOnlineDao.Properties.AssistantExamId.eq(assistantExamId))
                    .orderAsc(PagePictureOnlineDao.Properties.AnswerIndex)
                    .build()
                    .list();

        }catch (Exception e){
            e.printStackTrace();
        }
        if (list == null || list.size() == 0 ) {
            return null;
        }
        return list;
    }



    public PagePictureOnline getPagePictureOnlineByPictureLocal(String localPath) {
        PagePictureOnline PagePictureOnline = null;
        try {
            PagePictureOnline = getPagePictureOnlineDao().queryBuilder()
                    .where(PagePictureOnlineDao.Properties.LocalPath.eq(localPath))
                    .build()
                    .unique();

        }catch (Exception e){
            e.printStackTrace();
        }
        return PagePictureOnline;
    }

    public PagePictureOnline getPagePictureOnlineByPictureLocalClip(String localClipPath) {
        PagePictureOnline PagePictureOnline = null;
        try {
            PagePictureOnline = getPagePictureOnlineDao().queryBuilder()
                    .where(PagePictureOnlineDao.Properties.LocalClipPath.eq(localClipPath))
                    .build()
                    .unique();

        }catch (Exception e){
            e.printStackTrace();
        }
        return PagePictureOnline;
    }

    public synchronized void deletePagePictureOnlineByExamId(Long assistantExamId){
        try {
            List<PagePictureOnline> list = getPagePictureOnlineDao().queryBuilder()
                    .where(PagePictureOnlineDao.Properties.AssistantExamId.eq(assistantExamId))
                    .build()
                    .list();
            if(list == null) {
                return;
            }
            getPagePictureOnlineDao().deleteInTx(list);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public synchronized void updatePagePictureOnline(PagePictureOnline item)
    {
        getPagePictureOnlineDao().update(item);
    }

    /********************************AssistantExamOnlineDao***********************************************/
    private AssistantExamOnlineDao getAssistantExamOnlineDao() {
        return getDaoSession().getAssistantExamOnlineDao();
    }

    public synchronized long insertAssistantExamOnline(AssistantExamOnline item) {
        try {
            List<AssistantExamOnline> list = getAssistantExamOnlineDao().queryBuilder()
                    .where(AssistantExamOnlineDao.Properties.PublishId.eq(item.getPublishId()),
                            AssistantExamOnlineDao.Properties.ExamId.eq(item.getExamId()),
                            AssistantExamOnlineDao.Properties.StudentId.eq(item.getStudentId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getAssistantExamOnlineDao().deleteInTx(list);
                LogUtils.e("m1: insertAssistantExamOnline 插入数据库错误，已经有数据了");
                return -1;
            }
            return getAssistantExamOnlineDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public AssistantExamOnline getAssistantExamOnlineByPublishId(Integer publishId, Integer examId, String studentId) {
        AssistantExamOnline assistantExam = null;
        try {
            assistantExam = getAssistantExamOnlineDao().queryBuilder()
                    .where(AssistantExamOnlineDao.Properties.PublishId.eq(publishId),
                            AssistantExamOnlineDao.Properties.ExamId.eq(examId),
                            AssistantExamOnlineDao.Properties.StudentId.eq(studentId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return assistantExam;
    }


    public synchronized void updateAssistantExamOnline(AssistantExamOnline item) {
        getAssistantExamOnlineDao().update(item);
    }

    public synchronized void deleteAssistantExamOnlineByPublishId(Integer publishId, Integer examId, String studentId){
        try {
            List<AssistantExamOnline> list = getAssistantExamOnlineDao().queryBuilder()
                    .where(AssistantExamOnlineDao.Properties.PublishId.eq(publishId),
                            AssistantExamOnlineDao.Properties.ExamId.eq(examId),
                            AssistantExamOnlineDao.Properties.StudentId.eq(studentId))
                    .build()
                    .list();
            if(list == null) {
                return;
            }
            getAssistantExamOnlineDao().deleteInTx(list);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    /********************************WireExamDao***********************************************/
    private WireExamDao getWireExamDao() {
        return getDaoSession().getWireExamDao();
    }

    public synchronized long insertWireExam(WireExam item) {
        try {
            List<WireExam> list = getWireExamDao().queryBuilder()
                    .where(WireExamDao.Properties.ExamPublishId.eq(item.getExamPublishId()),
                            WireExamDao.Properties.StudentExamId.eq(item.getStudentExamId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getWireExamDao().deleteInTx(list);
                LogUtils.e("m1: insertWireExam 插入数据库错误，已经有数据了");
                return -1;
            }
            return getWireExamDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public WireExam getWireExamByExamPublishIdAndStudentExamId(Integer examPublishId, Integer studentExamId) {
        WireExam wireExam = null;
        try {
            wireExam = getWireExamDao().queryBuilder()
                    .where(WireExamDao.Properties.ExamPublishId.eq(examPublishId),
                            WireExamDao.Properties.StudentExamId.eq(studentExamId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return wireExam;
    }

    public WireExam getWireExamById(Long id) {
        WireExam wireExam = null;
        try {
            wireExam = getWireExamDao().queryBuilder()
                    .where(WireExamDao.Properties.Id.eq(id))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return wireExam;
    }

    public synchronized void deleteWireExamById(long id) {
        try {
            List<WireExam> list = getWireExamDao().queryBuilder()
                    .where(WireExamDao.Properties.Id.eq(id))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getWireExamDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updateWireExam(WireExam item) {
        getWireExamDao().update(item);
    }

    /********************************WireQuestionDao***********************************************/
    private WireQuestionDao getWireQuestionDao() {
        return getDaoSession().getWireQuestionDao();
    }

    public synchronized long insertWireQuestion(WireQuestion item) {
        try {
            List<WireQuestion> list = getWireQuestionDao().queryBuilder()
                    .where(WireQuestionDao.Properties.AssistantExamId.eq(item.getAssistantExamId()),
                            WireQuestionDao.Properties.QuestionId.eq(item.getQuestionId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getWireQuestionDao().deleteInTx(list);
                LogUtils.e("m1: insertWireQuestion 插入数据库错误，已经有数据了");
                return -1;
            }
            return getWireQuestionDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public WireQuestion getWireQuestionByAssistantExamIdAndQuestionId(Long assistantExamId, Integer questionId) {
        WireQuestion wireQuestion = null;
        try {
            wireQuestion = getWireQuestionDao().queryBuilder()
                    .where(WireQuestionDao.Properties.AssistantExamId.eq(assistantExamId),
                            WireQuestionDao.Properties.QuestionId.eq(questionId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return wireQuestion;
    }

    public synchronized void deleteWireQuestionById(long id) {
        try {
            List<WireQuestion> list = getWireQuestionDao().queryBuilder()
                    .where(WireQuestionDao.Properties.Id.eq(id))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getWireQuestionDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updateWireQuestion(WireQuestion item) {
        getWireQuestionDao().update(item);
    }

    /********************************WireQuestionPictureDao***********************************************/
    private WireQuestionPictureDao getWireQuestionPictureDao() {
        return getDaoSession().getWireQuestionPictureDao();
    }

    public synchronized long insertWireQuestionPicture(WireQuestionPicture item) {
        try {
            List<WireQuestionPicture> list = getWireQuestionPictureDao().queryBuilder()
                    .where(WireQuestionPictureDao.Properties.WireQuestionId.eq(item.getWireQuestionId()),
                            WireQuestionPictureDao.Properties.PageSerial.like(item.getPageSerial()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getWireQuestionDao().deleteInTx(list);
                LogUtils.e("m1: insertWireQuestionPicture 插入数据库错误，已经有数据了");
                return -1;
            }
            return getWireQuestionPictureDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public WireQuestionPicture getWireQuestionPictureByWireQuestionIdAndPageSerialAndPageIndex(Long wireQuestionId, String pageSerial, Integer pageIndex) {
        WireQuestionPicture wireQuestionPicture = null;
        try {
            wireQuestionPicture = getWireQuestionPictureDao().queryBuilder()
                    .where(WireQuestionPictureDao.Properties.WireQuestionId.eq(wireQuestionId),
                            WireQuestionPictureDao.Properties.PageSerial.like("%" + pageSerial + "%"),
                            WireQuestionPictureDao.Properties.PageIndex.eq(pageIndex))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return wireQuestionPicture;
    }

    public WireQuestionPicture getWireQuestionPictureById(Long id) {
        WireQuestionPicture wireQuestionPicture = null;
        try {
            wireQuestionPicture = getWireQuestionPictureDao().queryBuilder()
                    .where(WireQuestionPictureDao.Properties.Id.eq(id))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return wireQuestionPicture;
    }

    public synchronized void deleteWireQuestionPictureById(long id) {
        try {
            List<WireQuestionPicture> list = getWireQuestionPictureDao().queryBuilder()
                    .where(WireQuestionPictureDao.Properties.Id.eq(id))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getWireQuestionPictureDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void updateWireQuestionPicture(WireQuestionPicture item) {
        getWireQuestionPictureDao().update(item);
    }

    /********************************FlexiImgDao***********************************************/
    public FlexiImgDao getFlexiImgDao() {
        return getDaoSession().getFlexiImgDao();
    }

    public synchronized long insertFlexiImg(FlexiImg item) {
        try {
            return getFlexiImgDao().insertOrReplace(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public FlexiImg getFlexiImgByUrl(String url) {
        FlexiImg flexiImg = null;
        try {
            flexiImg = getFlexiImgDao().queryBuilder()
                    .where(FlexiImgDao.Properties.Url.like("%" + url + "%"))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return flexiImg;
    }

    public synchronized void deleteFlexiImgById(long id) {
        try {
            List<FlexiImg> list = getFlexiImgDao().queryBuilder()
                    .where(FlexiImgDao.Properties.Id.eq(id))
                    .build()
                    .list();
            if (list == null) {
                return;
            }
            getFlexiImgDao().deleteInTx(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public synchronized void updateFlexiImg(FlexiImg item) {
        getFlexiImgDao().update(item);
    }


    /********************************AnswerCardDao***********************************************/
    private AnswerCardDao getAnswerCardDao() {
        return getDaoSession().getAnswerCardDao();
    }

    public synchronized long insertAnswerCard(AnswerCard item) {
        try {
            List<AnswerCard> list = getAnswerCardDao().queryBuilder()
                    .where(AnswerCardDao.Properties.Pageserial.like(item.getPageserial()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getWireQuestionDao().deleteInTx(list);
                LogUtils.e("m1: insertAnswerCard 插入数据库错误，已经有数据了");
                return -1;
            }
            return getAnswerCardDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public AnswerCard getAnswerCardByPageSerial(String pageSerial) {
        AnswerCard answerCard = null;
        try {
            answerCard = getAnswerCardDao().queryBuilder()
                    .where(WireQuestionPictureDao.Properties.PageSerial.like("%" + pageSerial + "%"))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return answerCard;
    }

    public List<AnswerCard> getAnswerCard() {
        List<AnswerCard> answerCard = null;
        try {
            answerCard = getAnswerCardDao().queryBuilder().build()
                    .list();

        } catch (Exception e) {
            e.printStackTrace();
        }
        if(answerCard == null) {
            answerCard = new ArrayList<>();
        }
        return answerCard;
    }

    public synchronized void updateAnswerCard(AnswerCard item) {
        getAnswerCardDao().update(item);
    }

    /********************************PenOffLineGridInfoChildDao***********************************************/
    private PenOffLineGridInfoChildDao getPenOffLineGridInfoChildDao() {
        return getDaoSession().getPenOffLineGridInfoChildDao();
    }

    public synchronized long insertPenOffLineGridInfoChild(PenOffLineGridInfoChild item) {
        try {
            List<PenOffLineGridInfoChild> list = getPenOffLineGridInfoChildDao().queryBuilder()
                    .where(PenOffLineGridInfoChildDao.Properties.GridId.eq(item.getGridId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getWireQuestionDao().deleteInTx(list);
                LogUtils.e("m1: insertPenOffLineGridInfoChild 插入数据库错误，已经有数据了");
                return -1;
            }
            return getPenOffLineGridInfoChildDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }


    public synchronized void updatePenOffLineGridInfoChild(PenOffLineGridInfoChild item) {
        getPenOffLineGridInfoChildDao().update(item);
    }

    public synchronized void deletePenOffLineGridInfoChild(PenOffLineGridInfoChild item) {
        try {

            LogUtils.e("删除格子， deletePenOffLineGridInfoChild ");
            getPenOffLineGridInfoChildDao().delete(item);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /********************************StudentIdToEnDao***********************************************/
    private StudentIdToEnDao getStudentIdToEnDao() {
        return getDaoSession().getStudentIdToEnDao();
    }

    public synchronized long insertStudentIdToEn(StudentIdToEn item) {
        try {
            List<StudentIdToEn> list = getStudentIdToEnDao().queryBuilder()
                    .where(StudentIdToEnDao.Properties.StudentId.like(item.getStudentId()))
                    .build()
                    .list();
            if (list != null && list.size() > 0) {
                //getWireQuestionDao().deleteInTx(list);
                LogUtils.e("m1: insertStudentIdToEn 插入数据库错误，已经有数据了");
                return -1;
            }
            return getStudentIdToEnDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    public StudentIdToEn getStudentIdToEnByStudentId(String studentId) {
        StudentIdToEn studentIdToEn = null;
        try {
            studentIdToEn = getStudentIdToEnDao().queryBuilder()
                    .where(StudentIdToEnDao.Properties.StudentId.like(studentId))
                    .build()
                    .unique();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return studentIdToEn;
    }

    public synchronized void updateStudentIdToEn(StudentIdToEn item) {
        getStudentIdToEnDao().update(item);
    }




    /********************************PageGridDao***********************************************/
    private PageGridDao getPageGridDao() {
        return getDaoSession().getPageGridDao();
    }

    /********************************QuestionGridDao***********************************************/
    private QuestionGridDao getQuestionGridDao() {
        return getDaoSession().getQuestionGridDao();
    }

    /********************************QuestionRevisionItemDao***********************************************/
    private QuestionRevisionItemDao getQuestionRevisionItemDao() {
        return getDaoSession().getQuestionRevisionItemDao();
    }


    public synchronized void deleteAllTable(String studentId) {
        try{
            getAnswerCardDao().deleteAll();
            getAssistantExamDao().deleteAll();
            getAssistantExamOnlineDao().deleteAll();
            getAssistantQuestionDao().deleteAll();
            getAssistantQuestionOnlineDao().deleteAll();
            getExaminationPointItemDao().deleteAll();
            getExamListenDao().deleteAll();
            getHttpCacheItemDao().deleteAll();
            getNoticeItemDao().deleteAll();
            getPageGridDao().deleteAll();
            getPagePictureDao().deleteAll();
            getPagePictureGridDao().deleteAll();
            getPagePictureOnlineDao().deleteAll();
            getPenOffLineGridInfoDao().deleteAll();
            getPenOffLineGridInfoChildDao().deleteAll();
            getQuestionGridDao().deleteAll();
            getQuestionRevisionItemDao().deleteAll();
            getReadPushRecordDao().deleteAll();
            getUserItemDao().deleteAll();
            getWireExamDao().deleteAll();
            getWireQuestionDao().deleteAll();
            getWireQuestionPictureDao().deleteAll();

            String dirPath = Constants.FILE_PATH + File.separator + studentId;
            File fileDir = new File(dirPath);
            if(fileDir.exists() && fileDir.isDirectory()) {
                File[] files =  fileDir.listFiles(new MyFileFilter());
                for(File file : files) {
                    LogUtils.e("需要删除的文件夹：" + file.getPath());
                    //file.delete();
                    FileUtils.deleteDirWihtFile(file);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void clearBuffer() {
        getDaoSession().clear(); //清空所有表的缓存
        //getPenOffLineGridInfoChildDao().detachAll();//清空当前表的缓存
    }
}
